package kr.kimkisik.middle1math.gyesan;

import kr.kimkisik.middle1math.R;
import kr.kimkisik.middle1math.danwon.DanwonMain;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.nbpcorp.mobilead.sdk.MobileAdListener;
import com.nbpcorp.mobilead.sdk.MobileAdView;

public class Pyramid extends Activity implements MobileAdListener {
	private MobileAdView adView = null;
	EditText mEditgaro, mEditsero, mEdith;
	TextView mTextResult;
	InputMethodManager imm;
	Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.gyesan_pyramid);

		mEditgaro = (EditText) findViewById(R.id.pyramid_garo);
		mEditsero = (EditText) findViewById(R.id.pyramid_sero);
		mEdith = (EditText) findViewById(R.id.pyramid_h);
		mTextResult = (TextView) findViewById(R.id.pyramid_result);
		button = (Button) findViewById(R.id.pyramid_gyesangogo);
		adView = (MobileAdView) findViewById(R.id.adview1);
		adView.setListener(this);
		adView.start();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (adView != null) {
			adView.destroy(); // 10
			adView = null;
		}
	}

	@Override
	public void onReceive(int err) {
		// event for receive ad
	}

	public void mOnClick(View v) {
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		double mGARO = 0.0, mSERO = 0.0, mH = 0.0;
		try {
			mGARO = Double.parseDouble(mEditgaro.getText().toString());
			mSERO = Double.parseDouble(mEditsero.getText().toString());
			mH = Double.parseDouble(mEdith.getText().toString());
		} catch (Exception e) {
			Toast.makeText(this, "숫자를 입력하지 않으셨거나, \n 잘못입력하셨습니다.",
					Toast.LENGTH_SHORT).show();
		}
		switch (v.getId()) {
		case R.id.pyramid_gyesangogo:
			if (mGARO == 0.0 || mH == 0.0 || mSERO == 0.0) {
				Toast.makeText(this, "숫자를 입력하지 않으셨거나, \n 잘못입력하셨습니다.",
						Toast.LENGTH_SHORT).show();
			} else {
				imm.hideSoftInputFromWindow(button.getWindowToken(), 0);
				mTextResult
						.setText("▶ 밑면 삼각형의 가로(밑면) : "
								+ mEditgaro.getText()
								+ "\n▶ 밑면 삼각형의 세로(높이) : "
								+ mEditsero.getText()
								+ "\n▶ 삼각뿔의 높이  : "
								+ mEdith.getText()
								+ "\n☞ 삼각뿔의 부  피 : "
								+ (double) (((((mGARO * mSERO) / 2) * mH) / 3) * 100 + 0.5f)
								/ 100f);
				mTextResult.setTextColor(Color.WHITE);
			}
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent();
		switch (item.getItemId()) {

		case R.id.danwonmenu:
			intent.setClass(this, DanwonMain.class);
			startActivity(intent);
			return true;

		case R.id.gyesan:
			intent.setClass(this, GyesanMain.class);
			startActivity(intent);
			return true;

		}
		return false;
	}

}
